// index.js
const FormData = require('../../formData.js')
const app = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    activeItem:0,
    list: [
      {key: '星期一',value: '0'},
      {key: '星期二',value: '1'},
      {key: '星期三',value: '2'},
      {key: '星期四',value: '3'},
      {key: '星期五',value: '4'},
      {key: '星期六',value: '5'},
      {key: '星期天',value: '6'},
      {key: '建议',value: '7'},
    ],
    zlist: [],
    flist: [],
    wlist: [],
    curDay: 0,
    showModal: false,
    mc: '',
    stars1: [
      "../../static/image/starn.png",
      "../../static/image/starn.png",
      "../../static/image/starn.png",
      "../../static/image/starn.png",
      "../../static/image/starn.png",
    ],
    star_num: 0, 
    num: 0,
    imageUrls: [],
    showPic: true,
    content: '',
    mid: '',
    submf: true,
    jyModal: false,
    jylx: '',
    jycon: '',
    submjy: true,
    jysj: '',
    piclen: 3,
  },
  onLoad() {
    var zj = new Date().getDay();
    if(zj == 0){
      zj = 7;
    }
    this.setData({
      activeItem: zj - 1,
      curDay: zj - 1,
    })
    this.getWeekMenu(zj - 1);
  },
  clicknav (e) {
    let value = e.currentTarget.dataset.item;
    this.setData({
      activeItem: value,
    })
    if(value==7){
      return
    }
    this.getWeekMenu(value);
  },
  getWeekMenu (days) {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'http://42.228.11.30:41024/menu/getWeekMenu?zj='+days,
      method: 'POST',
      success: res => {
        wx.hideLoading();
        const data = res.data.data;
        this.setData({
           zlist: data.早,
           flist: data.午,
           wlist: data.晚,
        })
      
      }
    })
  },
  dp (e) {
    const mc = e.currentTarget.dataset.mc;
    const id = e.currentTarget.dataset.id;
    this.setData({
      showModal:true,
      mc: mc,
      mid: id,
      submf: true,
      star_num: 0,
      content: '',
      imageUrls: [],
      stars1: [
        "../../static/image/starn.png",
        "../../static/image/starn.png",
        "../../static/image/starn.png",
        "../../static/image/starn.png",
        "../../static/image/starn.png",
      ],
    })
  },
  jydp (e){
    const jylx = e.currentTarget.dataset.jylx;
    const jysj = e.currentTarget.dataset.jysj;
    this.setData({
      jyModal:true,
      submjy: true,
      jylx: jylx,
      jycon: '',
      jysj: jysj,
    })
  },
  gb(){
    this.setData({
      showModal:false
    })
  },
  jygb (){
    this.setData({
      jyModal:false
    })
  },
  score:function(e){
    var index=e.currentTarget.dataset.index;
    var len = this.data.stars1.length;
    for(var i=0;i<len;i++){
      if(i<=index){
        this.data.stars1[i] = '../../static/image/star1.png';
      }else{
        this.data.stars1[i] = '../../static/image/starn.png';
      }
    }
    this.setData({
      star_num: index+1,
      stars1: this.data.stars1
    })

  },
  preventTouchMove: function(){
    return false;
  },
  showPic() {
    wx.chooseImage({
      count: this.data.piclen, // 可选择的图片数量
      sizeType: ['compressed'], // 压缩图片
      sourceType: ['album', 'camera'], // 来源：相册或相机
      success:  (res)=> {
        let flag = true;

        if(this.data.imageUrls.length == 2){
          flag = false;
        };
        const len = this.data.piclen - res.tempFilePaths.length;
        if(len == 0){
          flag = false;
        };
        this.setData({
          imageUrls: this.data.imageUrls.concat(res.tempFilePaths),
          //imageUrls: this.data.imageUrls.concat(res.tempFilePaths[0]),
          showPic: flag,
          piclen: len,
        })
        
      }
    })
  },
  delpic(e){
    var index=e.currentTarget.dataset.idx;
    this.data.imageUrls.splice(index,1)
    this.setData({
      imageUrls: this.data.imageUrls,
      showPic: true,
      piclen: this.data.piclen + 1,
    });
  },
  sub(){
    const form =  new FormData();
    for(let i = 0;i<this.data.imageUrls.length;i++){
      form.appendFile("file", this.data.imageUrls[i], "t");
    }
    if(this.data.star_num == 0){
      wx.showToast({
        title: "请对饭菜进行评分！",
        icon: "none",
        duration: 2e3
      });
      return
    }
    form.append("score",this.data.star_num);

    if(this.data.content == ''){
      wx.showToast({
        title: "请填写评价内容！",
        icon: "none",
        duration: 2e3
      });
      return
    }
    if(this.data.content.length > 200 ){
      wx.showToast({
        title: "评价内容不能超过两百字！",
        icon: "none",
        duration: 2e3
      });
      return
    }
    form.append("msg",this.data.content);
    form.append("mid",this.data.mid)
    let data = form.getData();
    this.setData({
      submf: false,
    });

    var that = this;
    wx.request({
      method: "POST",
      url: "http://42.228.11.30:41024/fcdp/saveFcdp",
      header: {
        'content-type': data.contentType
      },
      data: data.buffer,
      success: res => {
        wx.showToast({
          title: "感谢您的点评",
          icon: "none",
          duration: 2e3,
          success: function() {
            setTimeout(function() {
              that.setData({
                showModal: false,
              });
            }, 1500);
          }
        });
      }
    });
  },
  jysub(){
    if(this.data.jycon == ''){
      wx.showToast({
        title: "请填写留言内容！",
        icon: "none",
        duration: 2e3
      });
      return
    }
    if(this.data.jycon.length > 200 ){
      wx.showToast({
        title: "留言内容不能超过两百字！",
        icon: "none",
        duration: 2e3
      });
      return
    }
    const form = {content: this.data.jycon,type: this.data.jylx};
    this.setData({
      submjy: false,
    });
    var that = this;
    wx.request({
      method: "POST",
      url: "http://42.228.11.30:41024/fcjy/saveFcjy",
      data: JSON.stringify(form),
      success: res => {
        wx.showToast({
          title: "感谢您的留言",
          icon: "none",
          duration: 2e3,
          success: function() {
            setTimeout(function() {
              that.setData({
                jyModal: false,
              });
            }, 1500);
          }
        });
      }
    });
  },
  bindText(e) {
    this.setData({
      content: e.detail.value
    });
  },
  bindJyText(e){
    this.setData({
      jycon: e.detail.value
    });
  },
  methods: {

  },
})
